package com.carkeeper.order.controller;
import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.order.pojo.Order;
import com.carkeeper.order.service.OrderService;
import com.carkeeper.order.service.OutstandingService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags = "待处理订单")
@RequestMapping("/outstanding")
public class OutstandingOrderController {
    @Autowired
   private OrderService orderService;

    @Autowired
    private OutstandingService outstandingService;

    //查询所有待处理订单
    @ApiOperation("分页条件查询所有待处理订单")
    @GetMapping(value = "/search/{page}/{size}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "订单编号", required = false),
            @ApiImplicitParam(name = "contact", value = "客户名字", required = false),
            @ApiImplicitParam(name = "storeName", value = "店铺名称", required = false),
            @ApiImplicitParam(name = "createDate", value = "订单时间", required = false),
    })
    public Result findPage(@RequestParam(required = false) Map searchMap , @PathVariable int page , @PathVariable int size){
        try {
            Page<Order> pageList = outstandingService.findPage(searchMap,page,size);
            PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
            return new Result(true, StatusCode.OK,"查询成功",pageResult);
        }catch (Exception e){
            return  new Result(false,StatusCode.OK,"查询失败");
        }

    }

    //关闭订单
    @ApiOperation("关闭订单")
    @PutMapping("/close/{id}")
    @ApiImplicitParam(name = "id",value = "订单id",required = true)
    public Result orderClose(@PathVariable("id") String id){
        try {
            outstandingService.closeOrder(id);
            return new Result(true,StatusCode.OK,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,StatusCode.OK,"修改失败");
        }
    }
}
